<link rel="stylesheet" href="/plug/layui/css/layui.css">
<div style="margin: 40px;">
    <form class="layui-form" xmlns:th="http://www.thymeleaf.org">
        <input type="hidden" name="id" th:value="${admin == null ? '' : admin.id}">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" maxlength="100" th:value="${admin == null ? '' : admin.username}" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" maxlength="100" placeholder="密码默认为123456" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <select name="sex">
                    <option value="0" th:selected="${admin != null && admin.sex == 0}">女</option>
                    <option value="1" th:selected="${admin != null && admin.sex == 1}">男</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="required|phone" maxlength="100" placeholder="请输入联系电话" th:value="${admin == null ? '' : admin.phone}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/plug/layui/layui.all.js"></script>
<script>
    //加载layui
    layui.use(['layer', 'form','element','upload'], function(){
        var layer = layui.layer
            ,form = layui.form
            ,element = layui.element;

        //layui提交
        form.on('submit(submit)', function(data){
            $.ajax({
                data:JSON.parse(JSON.stringify(data.field)),
                type:"post",
                url:"/admin/edit",
                success:function (rest) {
                    layer.msg(rest.msg);
                    if(rest.code === 200) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function () {
                    layer.msg("发生错误，请联系管理员");
                }
            });
            return false;
        });
    });
</script>